﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Windows.Forms;

namespace MauThuTabControl
{
    /// <summary>
    /// Interaction logic for DSXuatHang.xaml
    /// </summary>
    public partial class DSXuatHang : Page
    {
        public DSXuatHang()
        {
            InitializeComponent();
            LoadGW();
        }
        ConnectionDB cn = new ConnectionDB();
        public void LoadGW()
        {
            DataTable dt = cn.LoadData("SELECT dbo.QLXuatHang.soPhieuX, dbo.QLXuatHang.ngayXuat, dbo.QLXuatHang.ghiChu, dbo.QLXuatHang.tongTien, dbo.DMKhachHang.tenKh FROM dbo.QLXuatHang INNER JOIN dbo.DMKhachHang ON dbo.QLXuatHang.maKh = dbo.DMKhachHang.maKh");
            _dtgdonvitinh.ItemsSource = dt.DefaultView;
            if (dt.Rows.Count > 0)
            {
                _btxoa.IsEnabled = true;
                _btsua.IsEnabled = true;
            }
            else
            {
                _btxoa.IsEnabled = false;
                _btsua.IsEnabled = false;
            }
        }

        private void _btthem_Click(object sender, RoutedEventArgs e)
        {
            CTXuatHang frm = new CTXuatHang();
            frm._CoThaoTac = 1;
            frm.ShowDialog();
            LoadGW();
        }

        private void _bsua_Click(object sender, RoutedEventArgs e)
        {
            CTXuatHang frm = new CTXuatHang();
            frm._CoThaoTac = 2;
            int row = _dtgdonvitinh.SelectedIndex;
            if (row == -1)
            {
                System.Windows.Forms.MessageBox.Show("Bạn phải chọn một dòng cần sửa!!!", "Chú Ý!");
            }
            else
            {
                frm._KhoaChinh = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["soPhieuX"].ToString();
                frm.ShowDialog();
            }
            LoadGW();


        }

        private void _btxoa_Click(object sender, RoutedEventArgs e)
        {
            int row = _dtgdonvitinh.SelectedIndex;
            if (row == -1)
            {
                System.Windows.Forms.MessageBox.Show("Bạn phải chọn một dòng cần xóa!!!", "Chú Ý!");
            }
            else
            {
                if (System.Windows.Forms.MessageBox.Show("Bạn có muốn xóa dữ liệu đang chọn không!!!?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string soPhieu = ((DataRowView)_dtgdonvitinh.SelectedItem).Row["soPhieuX"].ToString();
                    cn._ThucThiCauTruyVan("delete from QLXuatHang where soPhieuX=N'" + soPhieu + "'");
                    LoadGW();
                }
                
            }
            
        }
    }
}
